package cometd.test.pages;

import java.util.LinkedList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.AjaxCallDecorator;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;

import cometd.test.components.LongRunningConnectionNotifier;

public class ChatPage extends WebPage
{
	String message;
	static int clicked=0;
	String nick="Guest "+System.currentTimeMillis()%100;

	static List<String> messages = new LinkedList<String>();
	{
		if( messages.isEmpty() )messages.add( "Welcome" );
	}
	
	public ChatPage() 
	{
		final WebMarkupContainer messagesContainer = new WebMarkupContainer("messagesContainer");
		add( messagesContainer );
		messagesContainer.setOutputMarkupId(true);
		ListView lv = new ListView<String>( "messages", new PropertyModel<List<String>>( this, "messages" )){

			@Override
			protected void populateItem(ListItem<String> item) 
			{
				item.add( new Label( "message", item.getModelObject() ).setEscapeModelStrings(false) );
			}
		};
		
		final Label clickedLabel = new Label( "clicked", new PropertyModel(this, "clicked" ) );
		add( clickedLabel );
		clickedLabel.setOutputMarkupId(true);

		final LongRunningConnectionNotifier notifier = new LongRunningConnectionNotifier("notifier", "5", "6" ){

			@Override
			public void onNotify( List<String> channels, AjaxRequestTarget target) 
			{
				System.out.println( "The ChatPage got notified about: "+channels );
				if( channels.contains( "5" ) )
					target.addComponent( messagesContainer );
				if( channels.contains( "6" ) )
					target.addComponent( clickedLabel );
					
			}
			
		};
		add( notifier );
		
		messagesContainer.add( lv );
		
		Form f = new Form( "form" );
		
		final TextField field = new TextField( "message", new PropertyModel( this, "message" ) );
		field.setOutputMarkupId(true);
		f.add( field );

		f.add( new AjaxFormSubmitBehavior( f, "onSubmit" ){

			@Override
			protected IAjaxCallDecorator getAjaxCallDecorator() 
			{
				return new AjaxCallDecorator()
				{
					@Override
					public CharSequence decorateScript(CharSequence script) {
						return super.decorateScript(script)+"return false;";
					}
				};
			}
			
			@Override
			protected void onSubmit(AjaxRequestTarget target) 
			{
				if( message==null || message.equals( "" ) )
					return;
				if( message.startsWith("/nick") )
				{
					nick = message.substring(6).trim();
					if( nick.length()>20 )nick.substring(0, 20);
				}
				else
				{
					messages.add( 0, "<span style='color:#dddddd'>"+nick+"</span>: "+message );
					while( messages.size()>25 )messages.remove(25);
					notifier.notify( "5" );
					target.addComponent(messagesContainer);

				}
				message ="";
				target.appendJavascript("$('#"+field.getMarkupId()+"').val('');" );
			}

			@Override
			protected void onError(AjaxRequestTarget arg0) {
				
				
			}
		});
		add( f );
		
		add( new AjaxLink("link"){

			@Override
			public void onClick(AjaxRequestTarget target) 
			{
				clicked++;
				notifier.notify( "6" );
				target.addComponent(clickedLabel);
			}
		}) ;
	}

}
